package com.tianjipms.controller;

import com.tianjipms.dao.ParkingMapper;
import com.tianjipms.dao.VehicleAccessMapper;
import com.tianjipms.dao.VehicleMapper;
import com.tianjipms.entity.*;
import com.tianjipms.service.BillService;
import com.tianjipms.service.ParkingService;
import com.tianjipms.service.VehicleService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/api/parking")
public class ParkingController {
    @Resource
    private ParkingService parkingService;
    @Resource
    private VehicleService vehicleService;
    @Resource
    private VehicleMapper vehicleMapper;
    @Resource
    private ParkingMapper parkingMapper;
    @Resource
    private VehicleAccessMapper vehicleAccessMapper;
    @Resource
    private BillService billService;
    private int parkingAll = 20;
    //实时停车场数据
    @GetMapping("/")
    public ParkingCount parkingData(){
        return parkingService.findParkingData(parkingAll);
    }
    //进入停车场
    @PostMapping("/enter")
    public Result enterParking(@RequestBody Vehicle vehicle){
        Parking parking = new Parking();
        ParkingCount parkingCount = parkingService.findParkingData(parkingAll);
        if(parkingCount.getSurplusParking() <= 0){
            return Result.error();
        }
        //车牌是否存在
        if(!vehicleService.existsByLicenseNumber(vehicle.getLicenseNumber())){
            vehicle.setUserId(0L);
            vehicle.setIsVip(0);
            vehicle.setLicenseNumber(vehicle.getLicenseNumber());
            vehicleService.save(vehicle);
        }
        Vehicle vehicleTemp = vehicleMapper.vehiclByLicenseNumber(vehicle.getLicenseNumber());
        //是否是内部车
        if(vehicleTemp.getIsVip() == 1){
            parking.setVehiclId(vehicleTemp.getId());
            parking.setEnterTime(new Date());
            parkingMapper.insert(parking);
        }else {
            //外部车
            parking.setVehiclId(vehicleTemp.getId());
            parking.setEnterTime(new Date());
            parkingMapper.insert(parking);
        }
        return Result.ok();
    }
    //出停车场
    @PostMapping("/leave")
    public Result leaveParking(@RequestBody Vehicle vehicle){
        Parking parking = parkingMapper.parkingByLicenseNumber(vehicle.getLicenseNumber());
        Vehicle leaveVehicle = vehicleMapper.vehiclByLicenseNumber(vehicle.getLicenseNumber());
        VehicleAccess vehicleAccess = new VehicleAccess();;
        vehicleAccess.setLicenseNumber(null);
        vehicleAccess.setEnterTime(parking.getEnterTime());
        Date date = new Date();
        vehicleAccess.setLeaveTime(date);
        BigDecimal price = new BigDecimal(0.00);
        //是否是内部车
        if(leaveVehicle.getIsVip() == 1){
            vehicleAccess.setPrice(price);
        }else {
            long parkingTime = date.getTime() - parking.getEnterTime().getTime();
            long durationHour = 0;
            if(parkingTime % (60 * 60 * 1000) == 0){
                durationHour = parkingTime /(60 * 60 * 1000);
            }else {
                durationHour = (parkingTime /(60 * 60 * 1000)) + 1;
            }
            if(durationHour <= 1){
                vehicleAccess.setPrice(price);
            }else{
                //超过一小时,每小时按2元一小时
                price = BigDecimal.valueOf(2 * (durationHour - 1));
                vehicleAccess.setPrice(price);
            }
            vehicleAccess.setPrice(price);
        }
        vehicleAccessMapper.insert(vehicleAccess);
        if(price.compareTo(BigDecimal.valueOf(0.00)) != 0){
            Bill bill = new Bill();
            bill.setUserId(leaveVehicle.getUserId());
            bill.setPayeeId(0L);
            bill.setMessage("停车费费用");
            bill.setCreateTime(new Date());
            bill.setStatus(0);
            bill.setPrice(price);
            billService.save(bill);
        }
        parkingMapper.deleteById(parking.getId());
        return Result.ok();
    }
}
